package com.sumainfor.pc.market.service.tenant.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.UtilsMapper;
import com.sumainfor.pc.market.dao.tenant.TcmktWorderEvaMapper;
import com.sumainfor.pc.market.service.tenant.TcmktWorderEvaService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 *  租赁预约评价
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-27 15:50
 **/
@Service
public class TcmktWorderEvaServiceImpl implements TcmktWorderEvaService {
    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(TcmktWorderEvaServiceImpl.class);

    @Autowired
    TcmktWorderEvaMapper tcmktWorderEvaMapper;

    @Autowired
    UtilsMapper utilsMapper;

    /**
    * <p>
    *  添加-租客租赁预约评价
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult AddWorderEva(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "1");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            String UUID;
            Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                put("uuid", "TenEvaId");
                put("database", Constants.DATA_SOURCE_TCMKT);
                put("surface", "tcmkt_worder_eva");
            }});
            if(uuid!=null){
                int number=Integer.valueOf(uuid.get("uuid").toString());
                UUID= ComUtils.getLocalTrmSeqNum("PJ",number);
            }else{
                UUID=ComUtils.getLocalTrmSeqNum("PJ",0);
            }
            params.put("TenEvaId",UUID);
            params.put("EvaTime", ConvertDateTime.getCurrentTime());
            Integer cout = tcmktWorderEvaMapper.AddWorderEva(params);
            if(cout<=0){
                result.putFailed("评价失败!");
            }else{
                result.putSuccess("评价成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }

    @Override
    public  JsonResult getWordEvaTmp(){
        JsonResult result = new JsonResult();
        List<Map<String,Object>> resultList = tcmktWorderEvaMapper.getWordEvaTmp();
        return result.put(resultList);
    }

    @Override
    public JsonResult getWorderEvaCount(Map<String,Object> params) {
        JsonResult result = new JsonResult();
        System.out.println("params"+params);
        Integer a = tcmktWorderEvaMapper.getWorderEvaCount(params);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("PingJia", a);
        return result.put(resultMap);
    }
}
